//
//  AppDelegate.swift
//  HDSwfitWeibo
//
//  Created by wty on 16/4/7.
//  Copyright © 2016年 wty. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //加载登陆账号用于自动登陆
        let account  = HDUserAccoutViewModel.userAccoutShare.userLogin
        
        
       //1.创建Window
        window  =  UIWindow.init(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        //2.给Window 添加根控制器
        window?.rootViewController = defaultController()
        //3.显示内容
        window?.makeKeyAndVisible()

        return true
    }
    
    //切换界面
    func switchViewController(isMain:Bool) {
        let controller = isMain ? HDMainTabBarController() :HDWelcomeViewController()
        window?.rootViewController = controller
    }
    
    
    //根据条件返回的控制器
    private func defaultController ()->UIViewController {
        //没有登录 返回首页
        if !HDUserAccoutViewModel.userAccoutShare.userLogin {
            return HDMainTabBarController()
        }else {//登录了
            //检查是否最新版本 如果是就展示新特性界面  如果不是就展示欢迎界面
            print(newVersion())
            return newVersion() ? HDNewFeatureViewController():HDWelcomeViewController()

    }
}
    //检查新版本
    private func newVersion ()->Bool {
        //1.获取程序当前的版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        //2.获取保存在沙盒之前的应用程序版本
        let sandboxVersionKey = " sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxVersionKey)
        //3.判断是否最新版本
        let newVersion = (currentVersion != sandboxVersion)
        //4.如果是就把最新版本存到沙盒中
        if newVersion {
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey:  sandboxVersionKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        //5.返回是不是最新版本
        return newVersion
        
    }
    func applicationDidEnterBackground(application: UIApplication) {
        //清除缓存
        HDStatusDAL.shareDAL.deleteCacheStatus()
    }
    




}

